package com.movitech.mobile.cz.base.sys.bizcontroller;


import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.movitech.mobile.cz.base.common.redis.RedisDBHelper;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.sys.bizvo.UserChangeRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.movitech.mobile.cz.base.common.util.CodeUtil;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.bizservice.LoginAndPwdService;
import com.movitech.mobile.cz.base.sys.bizvo.UserRequestVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/*
 * <p>
 * 采购外网平台登录忘记密码前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/login/public")
@Api(value = "采购外网平台登录忘记密码接口", description = "采购外网平台登录忘记密码接口")
public class LoginAndPwdController {
    @Autowired
    @Qualifier("loginAndPwdService")
    private LoginAndPwdService loginAndPwdService;


    /***
	 * 采购外网平台登录
	 * @param userRequestVo
	 * @return
	 */
    @PostMapping("/restLogin")
    @ApiOperation(
            value = "采购外网平台登录接口",
            notes = "采购外网平台登录接口",
            response = Boolean.class
    )
	public RestReturnResult restLogin(@RequestBody UserRequestVO userRequestVo, HttpServletResponse response, HttpServletRequest request) {
		RestReturnResult restResult=RestReturnResult.success();
		try {
			restResult=loginAndPwdService.restLogin(userRequestVo,response,request);
		}catch (Exception e){
			e.printStackTrace();
			restResult=RestReturnResult.error();
		}
		return  restResult;
	}

	/***
	 * 采购外网平台登录
	 * @param userRequestVo
	 * @return
	 */
    @PostMapping("/restFindPassword")
    @ApiOperation(
            value = "采购外网平台忘记密码",
            notes = "采购外网平台忘记密码",
            response = Boolean.class
    )
	public RestReturnResult restFindPassword(@RequestBody UserRequestVO userRequestVo) {
		RestReturnResult restResult=RestReturnResult.success();
    	try {
			restResult=loginAndPwdService.restFindPassword(userRequestVo);
		}catch (Exception e){
    		e.printStackTrace();
			restResult=RestReturnResult.error();
		}
		return  restResult;
	}

	/***
	 * 采购外网平台登录
	 * @param userChangeRequestVO
	 * @return
	 */
	@PostMapping("/changePassword")
	@ApiOperation(
			value = "采购外网平台修改密码",
			notes = "采购外网平台修改密码",
			response = Boolean.class
	)
	public RestReturnResult changePassword(HttpServletRequest request, HttpServletResponse response,@RequestBody UserChangeRequestVO userChangeRequestVO) {
		RestReturnResult restResult=RestReturnResult.success();
		try {
			restResult=loginAndPwdService.changePassword(request,response,userChangeRequestVO);
		}catch (RuntimeException e){
			e.printStackTrace();
			restResult=new RestReturnResult(CommonConstants.FAIL, e.getMessage());
		}
		return  restResult;
	}

	/***
	 * 图片验证码
	 * @return
	 */
	@GetMapping("/getGraphicCode/{phone}")
	@ApiOperation(
			value = "图片验证码",
			notes = "图片验证码",
			response = Boolean.class
	)
	public RestReturnResult getCode(HttpServletRequest req, HttpServletResponse resp,@PathVariable String phone) {
		RestReturnResult restResult=RestReturnResult.success();
		try {
			restResult=loginAndPwdService.getGraphicCode(req,resp,phone);
		}catch (Exception e){
			e.printStackTrace();
			restResult=RestReturnResult.error();
		}
		return  restResult;
	}

	/***
	 * 发送验证码
	 * @return
	 */
	@GetMapping("/sendSmsCode/{phone}")
	@ApiOperation(
			value = "发送验证码",
			notes = "发送验证码",
			response = Boolean.class
	)
	public RestReturnResult sendSmsCode(@PathVariable String phone) {
		RestReturnResult restResult=RestReturnResult.success();
		try {
			restResult=loginAndPwdService.sendSmsCode(phone);
		}catch (Exception e){
			e.printStackTrace();
			restResult=RestReturnResult.error();
		}
		return  restResult;
	}


}

